<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 广告报表数据
 *
 * @icon fa fa-circle-o
 */
class AdvertisingRoi extends Backend
{
    
    /**
     * Advertisingreport模型对象
     * @var \app\admin\model\Advertisingreport
     */
    protected $model = null;
    protected $dataLimit = 'auth';
    protected $dataLimitField = 'admin_id';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Advertisingreport;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $where2['type'] = 1;
            $list = $this->model
                ->where($where)
                ->where($where2)
                ->order($sort, $order)
                ->paginate($limit);
            $lists = $list->items();
            if($lists){
                foreach ($lists as $k=>$v){
                    $admin = Db::name('admin')->where('id', '=', $v['admin_id'])->find();
                    if($admin){
                        $lists[$k]['admin_name'] = $admin['nickname'];
                    }else{
                        $lists[$k]['admin_name'] = '管理员已删除';
                    }

//26		张书凡 02 03 04
//27		黄彬
//28		付齐  01
//29		邹洋
//30		敖辉 05
                    switch ($v['admin_id']) {
                        case '26':
                            $tags = $admin['username'];
                            break;
                        case '27':
                            $tags = 'huangbin';
                            break;
                        case '29':
                            $tags = 'ZY';
                            break;
                        case '30':
                            $tags = 'aohui';
                            break;
                        default:
                            $tags = 'Admin';
                    }
                    $datalike['likes'] = '%'.$admin['username'].'%';
                    $datalike['likes2'] = '%'.$tags.'%';
                    if (!empty($v['createtime'])) {
                        //算两天的销售额
                        $createtime2 = $v['createtime'] + 86400 - 1;
                        $whereat = array();
                        $wheres = array();

                        $whereat['c.created_at'] = array(array('egt',$v['createtime']),array('elt',$createtime2));
                        $wheres['c.financial_status'] = 'paid';
                        //统计单店铺
                        $wheres['ac.admin_id'] = $v['admin_id'];

                        $ordershopify = Db::name('fedition_ordershopify')
                            ->alias('c')
                            ->join('fa_fedition_accounturl ac', 'ac.id = c.accounturl', 'left')
                            ->where($whereat)
                            ->where($wheres)
                            ->sum('total_price');
                        $ordershopifycount = Db::name('fedition_ordershopify')
                            ->alias('c')
                            ->join('fa_fedition_accounturl ac', 'ac.id = c.accounturl', 'left')
                            ->where($whereat)
                            ->where($wheres)
                            ->count();
                        if (!empty($ordershopify)) {
                            $whereb['created_at'] = array(array('egt',$v['createtime']),array('elt',$createtime2));
                            $whereb['financial_status'] = 'paid';
                            $ordershopify = Db::name('fedition_ordershopify')
                                ->where($whereb)
                                ->where(function ($query) use ($datalike) {
                                    $query->where('user_id', 'like', $datalike['likes'])
                                        ->whereor('user_id', 'like', $datalike['likes2']);
                                })
                                ->sum('total_price');
                            $ordershopifycount = Db::name('fedition_ordershopify')
                                ->where($whereb)
                                ->where(function ($query) use ($datalike) {
                                    $query->where('user_id', 'like', $datalike['likes'])
                                        ->whereor('user_id', 'like', $datalike['likes2']);
                                })
                                ->count();
                        }
                    }
                    //销售额
                    $lists[$k]['daily_sales'] = 0;

                    //毛利
                    $lists[$k]['gross_profit'] = 0;
                    //roi
                    $lists[$k]['roi'] = '0';
                    $lists[$k]['ordershopifycount'] = $ordershopifycount;
                    if (!empty($lists[$k]['ordershopifycount'])) {
                        $lists[$k]['roi'] = number_format((float)$lists[$k]['amount'] / (float)$lists[$k]['ordershopifycount'], 3);
                    }
                    if (!empty($ordershopify)) {
                        //毛利 = 销售额 - 广告 - 物流
                        $lists[$k]['gross_profit'] = number_format((float)$ordershopify - $lists[$k]['logistics_costs'] - $lists[$k]['amount'], 3);
                        //ROI
//                        if (!empty($lists[$k]['amount'])) {
//                            $lists[$k]['roi'] = '1:'.number_format((float)$ordershopify / $lists[$k]['amount'], 3);
//                        }
                        //销售额
                        $lists[$k]['daily_sales'] = number_format($ordershopify,3);
                    }
                }
            }
            $result = array("total" => $list->total(), "rows" => $lists);

            return json($result);
        }
        return $this->view->fetch();
    }

}
